package com.wendy.common.core.json.jackson;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wendy.common.core.constant.BaseDbType;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: ALEX WANG
 * @Date: 2020/9/27 11:51
 */
public class JsonArrayList <T> extends ArrayList<T>
        implements BaseDbType<String> {

    public JsonArrayList() {
        super();
    }

    @Override
    public String selectDbTypeValue() {
        ObjectMapper mapper= new ObjectMapper();
        try {
            return mapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void addDbTypeValue(String c) {
        ObjectMapper mapper= new ObjectMapper();
        try {
            List<T> r = mapper.readValue(c, new TypeReference<List<T>>() {
            });
            this.addAll(r);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

    }
}
